// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <utility>
#include <cctype>
#include <sstream>
#include <string>

typedef std::pair<int, std::string> pair;

std::string s;
int n;
int len;

bool brace(int p)
{
	return s[p] == '(' || s[p] == ')';
}

bool op(int p)
{
	return s[p] == '+' || s[p] == '-' || s[p] == '*' || s[p] == '/';
}

bool dice(int p)
{
	int i = p;
	for (; i < len; i++) if (!isdigit(s[i])) break;
	if (i >= len) return false;
	return s[i] == 'd';
}

pair get_dice(int & p)
{
	pair ret;
	for (; p < len && isdigit(s[p]); p++)
		ret.first = ret.first * 10 + s[p] - '0';
	p++;
	for (; p < len && isdigit(s[p]); p++)
		ret.second += s[p];
	return ret;
}

std::string get_int(int & p)
{
	std::string ret;
	for (; p < len && isdigit(s[p]); p++)
		ret += s[p];
	return ret;
}

void print(std::string const & x)
{
	std::cout << "[d" << x << "]";
}

int main()
{
	std::cin >> n;
	std::getline(std::cin, s);
	while (n--) {
		std::string tmp;
		std::getline(std::cin, tmp);
		std::stringstream buf(tmp);
		s = "";
		for (std::string ts; buf >> ts; ) s += ts;
		int p = 0;
		len = s.size();
		for (; p < len; ) {
			if (brace(p)) std::cout << s[p++];
			else if (op(p)) std::cout << ' ' << s[p++] << ' ';
			else if (dice(p)) {
				pair tmp = get_dice(p);
				if (tmp.first > 1) std::cout << "(";
				print(tmp.second);
				for (int i = 1; i < tmp.first; i++) {
					std::cout << " + ";
					print(tmp.second);
				}
				if (tmp.first > 1) std::cout << ")";
			} else {
				std::string tmp = get_int(p);
				std::cout << tmp;
			}
		}
		std::cout << " = [Result]\n";
	}
}

